class Solution(object):
    def minMalwareSpread(self, graph, initial):
        n = len(graph)
        p = [i for i in range(n)]
        rank = [1] * n
        eyi_rank = [0] * n
        for index in initial:
            eyi_rank[index] += 1

        def find(x):
            if p[x] == x:
                return p[x]
            else:
                p[x] = find(p[x])
                return p[x]

        def union(x, y):
            rx = find(x)
            ry = find(y)
            if rx != ry:
                if rx > ry:
                    p[rx] = ry
                    rank[ry] += rank[rx]
                    rank[rx] = 0
                    eyi_rank[ry] += eyi_rank[rx]
                    eyi_rank[rx] = 0
                else:
                    p[ry] = rx
                    rank[rx] += rank[ry]
                    rank[ry] = 0
                    eyi_rank[rx] += eyi_rank[ry]
                    eyi_rank[ry] = 0

        for i in range(n):
            for j in range(i + 1, n):
                if graph[i][j]:
                    union(i, j)
        cut = 0
        ans = n
        for index in initial:
            root_index = find(index)
            if eyi_rank[root_index] >= 2:
                now_cut = 0
            else:
                now_cut = rank[root_index]
            if now_cut > cut:
                ans = index
                cut = now_cut
            else:
                if now_cut == cut:
                    ans = min(ans, index)
        return ans


data = Solution()
graph = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 1], [0, 0, 1, 1]]
initial = [3, 1]
print(data.minMalwareSpread(graph, initial))
